﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OUSMS.AppLogic.DataAccessLayer;
namespace OUSMS.AppLogic.BusinessLogicLayer
{
    [System.ComponentModel.DataObject(true)]
    public class MotionDepartmentsBLL : IDisposable
    {
        DatabaseContext _DatabaseContext;
        public MotionDepartmentsBLL():this(new DatabaseContext())
        { 
        }

        public MotionDepartmentsBLL(DatabaseContext DatabaseContext)
        {
            _DatabaseContext = DatabaseContext;
        }

        public void Dispose()
        {
            if (_DatabaseContext != null)
            {
                _DatabaseContext.Dispose();
                _DatabaseContext = null;
            }

            GC.SuppressFinalize(this);
        }

        public void AddMotionDepartment(MotionDepartments newAddedMotionDepartments)
        {
            _DatabaseContext.MotionDepartments.Add(newAddedMotionDepartments);
            _DatabaseContext.SaveChanges();
        }

        public MotionDepartments GetMotionDepartments(long motionID, int departmentID)
        {
            return _DatabaseContext.MotionDepartments.Where(m => m.MotionID == motionID && m.DepartmentID == departmentID).FirstOrDefault();
        }

        public void ClearMotionDepartments(long motionID)
        {
            IQueryable<MotionDepartments> motionDepartmentsList = _DatabaseContext.MotionDepartments.Where(m=>m.MotionID == motionID);
            foreach (MotionDepartments motionDepartment in motionDepartmentsList)
            {
                _DatabaseContext.MotionDepartments.Remove(motionDepartment);
            }
            _DatabaseContext.SaveChanges();
        }
    }
}